package com.service.business.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.base.controller.rep.CoinsRep;
import com.service.business.base.service.CoinService;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.mapper.CoinsMapper;
import com.service.dao.mapper.ProductHrComboTypeMapper;
import com.service.dao.model.Coins;
import com.service.dao.model.ProductHrComboType;
import com.service.utility.modelmapper.ModelMapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年09月27日</dd>
 * </dl>
 *
 * @author
 */
@Slf4j
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class CoinServiceImpl extends ServiceImpl<CoinsMapper, Coins> implements CoinService {

    @Autowired
    private ProductHrComboTypeMapper productHrComboTypeMapper;

    @Override
    public Map<String, BigDecimal> listCoinFee() {
        return baseMapper.selectAllByStatus(CommonStatusEnums.ACTIVE.getCode()).stream()
                .collect(Collectors.toMap(Coins::getCoinName, Coins::getCoinFee));
    }


    @Override
    public Integer isTransfer(Integer coinId) {
        Coins coins = baseMapper.selectById(coinId);
        return coins.getTransfer();
    }

    @Override
    public Coins getByCoinsName(String coinsName) {
        return baseMapper.selectOne(new QueryWrapper<Coins>().lambda()
                .eq(Coins::getCoinName, coinsName)
                .eq(Coins::getCoinStatus, CommonStatusEnums.ACTIVE.getCode())
        );
    }


    @Override
    public List<CoinsRep> getCoins() {
        List<Coins> list = this.list();
        List<CoinsRep> collect = this.list().stream().map(item -> ModelMapperUtil.map(item, CoinsRep.class)).collect(Collectors.toList());


        for (Coins coins : list) {
            List<ProductHrComboType> productHrComboTypes = productHrComboTypeMapper.selectList(new LambdaQueryWrapper<ProductHrComboType>().eq(ProductHrComboType::getCoinName, coins.getCoinName()));
            if (!ObjectUtils.isEmpty(productHrComboTypes) && productHrComboTypes.size() > 0) {
                for (CoinsRep coinsRep : collect) {
                    if (coinsRep.getCoinName().equals(coins.getCoinName())) {
                        coinsRep.setProductHrComboTypes(productHrComboTypes);
                    }
                }
            }
        }


        return collect;
    }

}
